using UnityEngine;
using System.Collections;

public class Crashable : MonoBehaviour {
	
	public GameObject explosion;
	public AudioClip explosionSound;
	
	void OnCollisionEnter(Collision collision) {
		Vector3 explosionPosition = collision.transform.position;
		if(collision.gameObject.tag=="Bomb")
		{ 
			print("Position of the explosion: " + explosionPosition.x + ", " + explosionPosition.y + ", " + explosionPosition.z);
			Tower[] towers  = (Tower[]) Object.FindObjectsOfType (typeof(Tower));
			for (int i=0;i<towers.Length;i++) {
				Vector3 towerPosition = towers[i].transform.position;
				//print("Position of tower " + i + ": " + towerPosition.x + ", " + towerPosition.y + ", " + towerPosition.z);
				float distance = Vector3.Distance(towerPosition, explosionPosition);
				if (distance < 2) {
					print("Distance from the explosion: " + distance);
					towers[i].Proximity();
				}
			}
			var effect = Instantiate(explosion, collision.gameObject.transform.position, Quaternion.identity);
			Destroy(collision.gameObject);
			AudioSource.PlayClipAtPoint(explosionSound, collision.gameObject.transform.position);
		}
	}
}
